package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.TableColumnPO;
import com.falcon.domain.dos.TableColumnDO;
import com.falcon.domain.req.TableColumnQuery;
import com.falcon.domain.req.TableColumnRequest;

import com.falcon.mapper.ITableColumnMapper;
import com.falcon.service.ITableColumnService;


@Slf4j
@Service
public class TableColumnService implements ITableColumnService {

    @Autowired
    private ITableColumnMapper tableColumnMapper;

    @Override
    public Long save(TableColumnRequest request){
        TableColumnPO tableColumnPO = BeanUtil.copyProperties(request, TableColumnPO.class);

        this.tableColumnMapper.insertNotNull(tableColumnPO);

        return tableColumnPO.getId();
    }

    @Override
    public Boolean update(Long id, TableColumnRequest request){

        TableColumnPO tableColumnPO = BeanUtil.copyProperties(request, TableColumnPO.class);
        tableColumnPO.setId(id);

        return this.tableColumnMapper.updateNotNull(tableColumnPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.tableColumnMapper.deleteById(id);
    }

    @Override
    public TableColumnDO queryById(Long id){

        TableColumnPO tableColumnPO = this.tableColumnMapper.selectById(id);

        TableColumnDO tableColumnDO = BeanUtil.copyProperties(tableColumnPO, TableColumnDO.class);

        return tableColumnDO;
    }


    @Override
    public List<TableColumnDO> queryList(TableColumnQuery request){

        Condition condition = request.toCondition();

        List<TableColumnPO> poList = this.tableColumnMapper.selectList(condition);

        List<TableColumnDO> doList = BeanUtil.copyToList(poList, TableColumnDO.class);

        return doList;
    }

    @Override
    public BasePager<TableColumnDO> queryPager(TableColumnQuery request){


        PageInfo<TableColumnPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.tableColumnMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, TableColumnDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.tableColumnMapper.deleteByIds(ids);
    }


}
